package com.example.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 扎马斯
 * @date: 2024年08月29日 17:18
 * @describe: 延时队列
 */
@Configuration
public class RabbitMqDead {

    @Bean
    public Queue queueDead() {
        Map<String, Object> deadMap = new HashMap<>();
        deadMap.put("x-message-ttl", 60000);
        return QueueBuilder.durable(RabbitMqConf.QUEUE_DEAD)
                .deadLetterExchange(RabbitMqConf.EXCHANGE_DEAD)
                .deadLetterRoutingKey(RabbitMqConf.ROUTING_KEY_DEAD)
                .withArguments(deadMap).build();
    }

    @Bean
    public Queue queueNormal() {
        return QueueBuilder.durable(RabbitMqConf.QUEUE_NORMAL).build();
    }

    @Bean
    public DirectExchange exchangeAndDead() {
        return ExchangeBuilder.directExchange(RabbitMqConf.EXCHANGE_DEAD).build();
    }

    @Bean
    public Binding bindingQueueDead(@Qualifier("queueDead") Queue queueDead, @Qualifier("exchangeAndDead") DirectExchange exchangeAndDead) {
        return BindingBuilder.bind(queueDead).to(exchangeAndDead).with(RabbitMqConf.ROUTING_KEY_DEAD);
    }

    @Bean
    public Binding bindingQueueDead1(@Qualifier("queueNormal") Queue queueNormal, @Qualifier("exchangeAndDead") DirectExchange exchangeAndDead) {
        return BindingBuilder.bind(queueNormal).to(exchangeAndDead).with(RabbitMqConf.ROUTING_KEY);
    }
}
